package com.karson.front.service.Impl;

import com.karson.common.constants.SmsConstants;
import com.karson.common.util.AsmrUtil;
import com.karson.common.util.ValidationUtil;
import com.karson.front.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public boolean sendCommonCode(String phone) {
        // 手机号验证通过
        ValidationUtil.isPhone(phone,"手机号码错误");
        return sendSms(phone, SmsConstants.SMS_CONFIG_TYPE_VERIFICATION_CODE);
    }

    private boolean sendSms(String phone, int tag) {
        Integer code = AsmrUtil.randomCount(111111, 999999);
        System.out.println("短信发送成功:"+ code);
        String key = SmsConstants.SMS_VALIDATE_PHONE + phone;
        redisTemplate.opsForValue().set(key, String.valueOf(code),SmsConstants.SMS_CONFIG_VERIFICATION_CODE_TTL, TimeUnit.SECONDS);
        return true;
    }
}
